<%@ page language="java" import="java.util.*,com.knife.member.*,tools.MD5,java.util.Date,java.text.SimpleDateFormat"
	pageEncoding="UTF-8"%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
String msg="";
String tid="";
boolean isOk=true;
boolean isLogin=false;
boolean isMacBind=false;
Userinfo auser=null;
UserinfoDAO userDAO=null;
String url="";
String email="";
String password="";
String checkpass="";
String mac="";
int macnums=1;
String retUrl="/index.do";
java.util.Date d = new java.util.Date();
java.text.SimpleDateFormat dformat = new java.text.SimpleDateFormat("yyyy-MM-dd");
String dt1 = dformat.format(d);

//检查验证码
String randomCode = (String) session.getAttribute("valiCode");
String submitrandom = request.getParameter("vcode");
if(submitrandom!=null){
	if(!submitrandom.equalsIgnoreCase(randomCode)){
		msg="验证码不匹配:"+randomCode;
		isOk=false;
	}
}else{
	msg="非法的提交";
	isOk=false;
}

if(request.getParameter("email")!=null){
	email 		= request.getParameter("email");
}
if(request.getParameter("password")!=null){
	password 	= request.getParameter("password");
}
if(request.getParameter("mac")!=null){
	mac 		= request.getParameter("mac");
}
Boolean issave		= Boolean.parseBoolean(request.getParameter("saveUserInfo"));

if(request.getParameter("url")!=null){
	url			= request.getParameter("url");
}
if(request.getParameter("tid")!=null){
	tid			= request.getParameter("tid");
}
//判断用户是否已登录
if(isOk){
	String loginname="";
	try{
		List UserSessions=com.knife.tools.SessionUtil.getUserList();
		if (UserSessions.size()>0) {
			for (int i = 0; i < UserSessions.size(); i++) {
				loginname=(String)UserSessions.get(i);
				System.out.println("在线用户:"+loginname);
				if(loginname.equalsIgnoreCase(email)){
						Map userMaps = com.knife.tools.SessionUtil.getUserSessions();
						HttpSession kickuser = (HttpSession)userMaps.get(email);
						if(kickuser!=null){
							kickuser.removeAttribute("loginname");
							kickuser.invalidate();
						}else{
							//如果session已经失效,直接移出名单
							 com.knife.tools.SessionUtil.RemoveUser(email);
						}
					isOk=true;
					break;
				}
			}
		}
	}catch(Exception e){
		//msg="请不要重复登录！";
		//isOk=false;
		e.printStackTrace();
	}
}

//检查是否有网卡绑定
//2011-12-16注释
/*
if(mac.trim().length()<=0){
	msg="未找到网卡，请安装客户端插件！";
	isOk=false;
	isLogin=false;
}else{
	isOk=true;
}
*/

if(isOk){
	userDAO = new UserinfoDAO();
	try{
		List<Userinfo> users = userDAO.findActiveByEmail(email);
		if(users.size()>0){
			auser=users.get(0);
		}
	}catch(Exception e){
		msg="登陆失败！无效的用户！";
		isLogin=false;
		e.printStackTrace();
	}
	if(auser!=null){
		String sysj = dformat.format(auser.getSysj());
        int result=dt1.compareTo(sysj);
	//if(auser.getPassword().equals(password)){
	if(CheckPassword.validatePassword(auser.getPassword(), password)){
		checkpass = password;
		password=auser.getPassword();
		isLogin=true;
		/*
		如果用户第二次登录密码依然是123456，则将此用户冻结
		if(checkpass.equals("123456") && auser.getLogincount()==0){
			auser.setActive(0);
			userDAO.update(auser);
		}
		*/
/*
		if(checkpass.equals("123456") && auser.getLogincount()>0){
			auser.setActive(0);
			auser.setLogincount(auser.getLogincount()+1);
			isLogin=false;
			userDAO.update(auser);
		}else{
			auser.setLogincount(auser.getLogincount()+1);	
			userDAO.update(auser);
		}
*/		
		try{
		//如果是免费用户,默认不绑定网卡
		if(auser.getIsfee()<=0){
			macnums=0;
		}
		//判断是否绑定网卡
		if(auser.getIsfee()>0){
			Usertype ausertype=(new UsertypeDAO()).findById(auser.getIsfee());
			if(ausertype.getMacnum()!=null){
				if(ausertype.getMacnum()>1){
					macnums=ausertype.getMacnum();
				}
			}
		}
		//取消绑定网卡开始2011-12-16加
		isMacBind=true;
		macnums=0;
		//取消绑定网卡结束
		if(macnums>0){
			//如果已绑定了网卡
			if(auser.getMac()!=null){
			if(auser.getMac().trim().length()>0){
				String[] macs=auser.getMac().split(",");
				//System.out.println("bbbbbbbbbbbbbbbbbbbbbb");
					//如果已经用完了所有绑定名额
					if(macnums<=macs.length){
						//System.out.println("ccccccccccccccccccccc");
						for(String amac:macs){
							//System.out.println("dddddddddddddddddddddddd");
							if(mac.trim().length()>0 && amac.trim().length()>0 && mac.equals(amac)){
								System.out.println("e:"+mac+"+"+amac);
								isMacBind=true;
								break;
							}
						}
					}else{
						auser.setMac(auser.getMac()+","+mac);
						userDAO.save(auser);
						isMacBind=true;
					}
			}else{
				auser.setMac(mac);
				userDAO.save(auser);
				isMacBind=true;
			}
			}
			//如果未绑定网卡
			else{
				auser.setMac(mac);
				userDAO.save(auser);
				isMacBind=true;
			}
			}
		}catch(Exception e){
			//e.printStackTrace();
			msg="登陆失败！绑定网卡失败！";
			isLogin=false;
		}
		

if(isMacBind){


	            		
if(result>0 )
{msg="登陆失败！超出试用时间！";
		isLogin=false;}

else{
			if(!request.isRequestedSessionIdValid()){
				session=request.getSession(true);
			}
			session.setAttribute("loginuser",email);	
			
			System.out.println("设置在线名称:"+email);
			//msg="登陆成功！";
			isLogin=true;
			Cookie loginUser = new Cookie("loginuser",email);
			Cookie loginPassword = new Cookie("loginpass",MD5.getMD5(password.getBytes()));
			if(issave){
				loginUser.setMaxAge(Integer.MAX_VALUE);
				loginPassword.setMaxAge(Integer.MAX_VALUE);
			}else{
				loginUser.setMaxAge(30*60);
				loginPassword.setMaxAge(30*60);
			}
			loginUser.setPath("/");
			loginPassword.setPath("/");
			response.addCookie(loginUser);
			response.addCookie(loginPassword);
		}

}else{
			msg="登陆失败！请不要试图在未绑定网卡的机器上访问客户端";
			isLogin=false;
		}
	}else{
		//session.removeAttribute("uname");
		msg="登陆失败！用户名密码错误！";
		isLogin=false;
	}
	}else{
		//session.removeAttribute("uname");
		msg="登陆失败！不存在的用户！";
		isLogin=false;
	}
}
%>
<script language="javascript">
<%
if(!isLogin){
%>
alert('<%=msg%>');
history.back(-1);
<%}else{
	auser.setLogincount(auser.getLogincount()+1);
	userDAO.update(auser);
if(tid.length()>0){
	retUrl="/type.do?tid="+tid;
}
if(auser.getLogincount()==1){
	retUrl="/user/index.jsp";
}else{
	retUrl="/point.do?parameter=list&tid=3723461329215900";	
}
%>
location.href='<%=retUrl%>';
<%
}%>
</script>